package com.wang.android.library.ui;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wang.android.library.network.eventbus.WHttpEventBean;
import com.wang.android.library.tool.WEventCache;
import com.wang.android.library.ui.dialog.WLoadingDialog;
import com.wang.android.library.ui.dialog.WNetLoadingDialog;

import butterknife.ButterKnife;

/**
 * Created by WangHuanyu on 2018/1/29.
 */

public abstract class FBaseDaggerFragment extends Fragment implements IFBaseView
{
    protected View mRootView;
    private WLoadingDialog loadingDialog;//dialog
    private WNetLoadingDialog wNetLoadingDialog;
//    public T presenter;
    public Intent intent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
//        presenter = WTextUtils.getT(this,0);
//        if(presenter != null){
//            presenter.setContext(getActivity());
//        }
        register();
        super.onCreate(savedInstanceState);
    }

    @Override
    public final View onCreateView(LayoutInflater inflater,
                                   ViewGroup container, Bundle savedInstanceState) {
        if (mRootView == null) {
            mRootView = inflater.inflate(getLayoutResource(), container, false);
        }
        ButterKnife.bind(this, mRootView);
        ViewGroup parentView = (ViewGroup) mRootView.getParent();
        if (parentView != null) {
            parentView.removeView(mRootView);
        }
        onInit(mRootView);
        setListeners();
        initTodo(savedInstanceState);
        return mRootView;
    }

    public abstract int getLayoutResource();
    /**
     * 预留方法单独注入
     * @param view
     */
    public abstract void onInit(View view);
    /**
     * onCreate中操作
     */
    protected abstract void initTodo(Bundle savedInstanceState);
    /**
     * 空方法监听设置（若使用ButterKnife请忽略）
     */
    protected void setListeners(){

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregister();
    }
    @Override
    public void dismissBaseDialog() {
        if (null != loadingDialog && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }

    @Override
    public void showBaseDialog() {
        boolean dialogStatus = false;
        if (null != loadingDialog) {
            dialogStatus = loadingDialog.isShowing();
        }
        if (!dialogStatus) {
            loadingDialog = WLoadingDialog.show(getActivity(), false, new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {

                }
            });
        }
    }

    @Override
    public void showNetDialog() {
        boolean dialogStatus = false;
        if (null != wNetLoadingDialog) {
            dialogStatus = wNetLoadingDialog.isShowing();
        }
        if (!dialogStatus) {
            wNetLoadingDialog = WNetLoadingDialog.show(getContext(), false, new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {

                }
            });
        }
    }

    @Override
    public void dismissNetDialog() {
        if (null != wNetLoadingDialog && wNetLoadingDialog.isShowing()) {
            wNetLoadingDialog.dismiss();
        }
    }

    @Override
    public void onEvent(WHttpEventBean wNetEventBean) {

    }

    @Override
    public void register() {
        WEventCache.eventBus.unregister(this);
        WEventCache.eventBus.register(this);
    }

    @Override
    public void unregister() {
        WEventCache.eventBus.unregister(this);
    }

    @Override
    public void stateError() {

    }

    @Override
    public void showErrorMsg(String msg) {

    }
}
